package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.UserDao;
import com.njtech.pet.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-13 13:35
 * Description UserDao测试类
 */
public class UserDaoExample {
    private static UserDao userDao;
    private static Scanner scanner;
    static public void insert() {
        System.out.println("about to test inserting a new user");
        System.out.println("creating...");
        User user = new User();
        user.setUsername("green boy");
        user.setHeadUrl("/");
        user.setPwd("test pwd, should be encrypted");
        user.setSex(Constant.User.SEX_FEMALE.getValue());
        user.setCredit(0);
        user.setPhone("136-6666-6666");
        user.setState(Constant.User.STATE_VALID.getValue());
        user.setLevel(Constant.Member.LEVEL0.getKey());
        System.out.println("inserting...");
        userDao.insert(user);
        System.out.println("insert finished,please check database");

    }
    static public void update(int id) {
        System.out.println("about to test updating a user");
        System.out.println("creating...");
        User user = new User();
        user.setId(id);
        user.setUsername("updated boy");
        user.setHeadUrl("/update");
        user.setPwd("update pwd, should be encrypted");
        user.setSex(Constant.User.SEX_FEMALE.getValue());
        user.setCredit(200);
        user.setPhone("136-upda-ted6");
        user.setState(Constant.User.STATE_VALID.getValue());
        user.setLevel(Constant.Member.LEVEL0.getKey());
        System.out.println("updating...");
        userDao.updateByUserId(user);
        System.out.println("update finished,please check database");

    }

    public static void select() {
        int id;
        System.out.println("please enter select id:");
        id = scanner.nextInt();
        User user = new User();
        user.setId(id);
        System.out.println("selecting...");
        System.out.println(userDao.selectByUserNameAndUserId(user));

        System.out.println("testing select by username");

        User user1 = new User();
        user1.setUsername("green boy");
        System.out.println("selecting");
        System.out.println(userDao.selectByUserNameAndUserId(user1));

    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        userDao = (UserDao) context.getBean("userDao");
        System.out.println("准备开始测试" + userDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit");
        System.out.println("1: test insert.");
        System.out.println("2: test update");
        System.out.println("3: test select");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert(); break;
                case 2:
                    System.out.println("enter user's id to update:");
                    int id = scanner.nextInt();
                    update(id);
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }
    }

    @Before
    public void prepare() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        userDao = (UserDao) context.getBean("userDao");
        System.out.println("准备开始测试" + userDao.toString());
    }

    @Test
    public void testSelectById() {
        System.out.println(userDao.selectByUserId(1));
    }

    @Test
    public void testUpdateCredit() {
        userDao.updateUserCredit(1, -150);
    }

    @Test
    public void selectElasticly(){
        User user = new User();
        user.setUsername("e");
        user.setId(1);
        System.out.println(userDao.selectElasticly(user,null,null,"2020-04-01",null));
    }
}
